﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class UGUITool
{
    private static Canvas _canvas;
    private static Canvas Canvas
    {
        get
        {
            if (_canvas == null)
                _canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
            return _canvas;
        }
    }

    public static Vector3 WorldPointToUGUIPoint(Canvas canvas, Vector3 worldPosition)
    {
        Vector3 v_v3 = Camera.main.WorldToScreenPoint(worldPosition);
        Vector3 v_ui = canvas.worldCamera.ScreenToWorldPoint(v_v3);
        Vector3 v_new = new Vector3(v_ui.x, v_ui.y, canvas.GetComponent<RectTransform>().anchoredPosition3D.z);
        return v_new;
    }

    public static Vector3 ScreenPointToUGUIPoint(Canvas canvas, Vector2 screenPosition)
    {
        Vector3 v_ui = canvas.worldCamera.ScreenToWorldPoint(screenPosition);
        Vector3 v_new = new Vector3(v_ui.x, v_ui.y, canvas.GetComponent<RectTransform>().anchoredPosition3D.z);
        return v_new;
    }

    /// <summary>
    /// 屏幕坐标-》canvas坐标
    /// </summary>
    /// <param name="cvs"></param>
    /// <param name="ScreenPos"></param>
    /// <param name="RectTransPos"></param>
    /// <returns></returns>
    public static bool GetRectPosForScreenPos(Canvas cvs, Vector2 ScreenPos, out Vector2 RectTransPos)
    {

        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(cvs.transform as RectTransform, ScreenPos, cvs.worldCamera, out RectTransPos))
        {
            return true;
        }

        return false;

    }

    /// <summary>
    /// 世界坐标-》canvas坐标
    /// </summary>
    /// <param name="cvs"></param>
    /// <param name="worldPos"></param>
    /// <param name="RectTransPos"></param>
    /// <returns></returns>
    public static bool GetRectPosForWorldPos(Canvas cvs, Vector3 worldPos, out Vector2 RectTransPos)
    {
        Vector2 ScreenPos = Camera.main.WorldToScreenPoint(worldPos);
        Vector2 finalScreenPos = new Vector2(ScreenPos.x, ScreenPos.y);

        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(cvs.transform as RectTransform, finalScreenPos, cvs.worldCamera, out RectTransPos))
        {
            return true;
        }

        return false;

    }

    /// <summary>
    /// 其它层级ui坐标-》Canvas根节点anchorPosition
    /// </summary>
    /// <param name="cvs"></param>
    /// <param name="rect"></param>
    /// <returns></returns>
    public static Vector2 GetCanvasAnchorLocalPosition(Canvas cvs, RectTransform rect)
    {
        Vector2 localpos = cvs.GetComponent<RectTransform>().InverseTransformPoint(rect.position);
        Debug.Log(localpos);
        return localpos;
    }

    public static Vector3 GetCanvasLocalPos(Canvas cvs, RectTransform target)
    {
        Canvas cv = cvs == null ? Canvas : cvs;

        return cv.transform.InverseTransformPoint(target.position);
    }

    public static bool GetCanvaLocalPos(Canvas cvs, RectTransform target, Camera cam, out Vector2 localPosition)
    {
        Canvas cv = cvs == null ? Canvas : cvs;
        Camera camera = cam == null ? Camera.main : cam;
        Vector2 _localpos;

        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(cv.transform as RectTransform, camera.WorldToScreenPoint(target.position), camera, out _localpos))
        {
            localPosition = _localpos;
            return true;
        }

        localPosition = Vector2.zero;
        return false;
    }

    //UGUI 提供了一个检测是否点击在UI上的方法
    //EventSystem.current.IsPointerOverGameObject();
    //但是该方法在PC上检测正常，结果拿到Android真机测试上，永远检测不到。
    public static bool IsPointerOverGameObject()
    {
        //https://blog.csdn.net/andyhebear/article/details/51433748
        PointerEventData eventData = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
        eventData.pressPosition = Input.mousePosition;
        eventData.position = Input.mousePosition;
        List<RaycastResult> list = new List<RaycastResult>();
        UnityEngine.EventSystems.EventSystem.current.RaycastAll(eventData, list);
        //Debug.Log(list.Count);
        return list.Count > 0;
        // }
    }

    /// <summary>
    /// 获取一系列ui组合的bound
    /// </summary>
    /// <param name="rts"></param>
    /// <param name="lu"></param>
    /// <param name="rd"></param>
    /// <param name="center"></param>
    public static void GetUGUIElementBound(List<RectTransform> rts, out Vector2 lu, out Vector2 rd, out Vector2 center)
    {
        Vector2 old_leftup, old_rightdown;
        old_leftup = old_rightdown = rts[0].anchoredPosition;
        Vector2 leftup, rightdown;
        for (int i = 0; i < rts.Count; i++)
        {
            GetUGUIElementBorder(rts[i], out leftup, out rightdown);
            if (leftup.x < old_leftup.x) old_leftup.x = leftup.x;
            if (leftup.y >= old_leftup.y) old_leftup.y = leftup.y;
            if (rightdown.y < old_rightdown.y) old_rightdown.y = rightdown.y;
            if (rightdown.x >= old_rightdown.x) old_rightdown.x = rightdown.x;
        }

        lu = old_leftup;
        rd = old_rightdown;
        center = (lu + rd) / 2f;
    }

    private static void GetUGUIElementBorder(RectTransform rt, out Vector2 leftup, out Vector2 rightdown)
    {
        leftup = new Vector2(rt.anchoredPosition.x - rt.sizeDelta.x / 2, rt.anchoredPosition.y + rt.sizeDelta.y / 2);
        rightdown = new Vector2(rt.anchoredPosition.x + rt.sizeDelta.x / 2, rt.anchoredPosition.y - rt.sizeDelta.y / 2);
    }

}
